package client.application;

import java.awt.event.ActionEvent;
import java.io.IOException;
import java.rmi.RemoteException;
import java.util.Collection;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.AbstractAction;

import client.ClientView;

/*
 * Author : Mohamed Delhomme
 */

public class ConnectAction extends AbstractAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ClientView client;

	public ConnectAction(ClientView client, String texte) {
		super(texte);

		this.client = client;
	}

	public void actionPerformed(ActionEvent e) {
		
		try {
			client.remote.connectToService("music_service._udp.localhost.");
		} catch (IOException e1) {
			e1.printStackTrace();
		} catch (InterruptedException e1) {
			e1.printStackTrace();
		}
		
		try {
			if (client.remote.isConnected())
			{
				client.buttonConnectServer.setEnabled(false);
				client.buttonDisconnectServer.setEnabled(true);
				client.connectItem.setEnabled(false);
				client.disconnectItem.setEnabled(true);
				client.labelLinkServer
						.setText("   Status : you are currently connected   ");
				
				client.connectItem.setEnabled(false);
				client.disconnectItem.setEnabled(true);	
				
				try {
					client.listSongs = client.remote.getListOfSongs();
				} catch (RemoteException e1) {
					e1.printStackTrace();
				}
				
				int numOfSongs = client.remote.getNumberOfSongs();
				Vector<String> listOfSongs = client.getStringSong(client.listSongs);
				
				client.listSongsAvailable.setListData(listOfSongs);
			}
		} catch (InterruptedException e1) {
			e1.printStackTrace();
		}

	}

}
